package com.shawn.baseappbygroovydsl.pop

import android.content.Context
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.shawn.baseappbygroovydsl.R
import com.shawn.baseappbygroovydsl.appInterface.IPopBtnActionListener
import com.shawn.baseappbygroovydsl.bean.BeanWechat50
import com.shawn.baseappbygroovydsl.databinding.PopWechat50RewardVideoBinding
import com.shawn.baseappbygroovydsl.ui.adapter.AdapterWechat50


/**
 * @author Secret
 * @since 2023/6/2
 */
class PopWechat50RewardVideo(context: Context) : PopBase(context),
    AdapterWechat50.AnimationCompleteListener {

    var iPopBtnActionListener: IPopBtnActionListener? = null
    private var bind: PopWechat50RewardVideoBinding
    private val textColor333 = "#333333"
    private val textColor999 = "#999999"

    private var adapterWechat50: AdapterWechat50? = null

    companion object {
        const val POP_WECHAT_50_REWARD_VIDEO_CLOSE = "pop_wechat_50_reward_video_close"
        const val POP_WECHAT_50_REWARD_VIDEO_BIND_WECHAT = "pop_wechat_50_reward_video_bind_wechat"
        const val POP_WECHAT_50_REWARD_VIDEO_GET = "pop_wechat_50_reward_video_get"
        const val POP_WECHAT_50_REWARD_VIDEO_SHOW_AD = "pop_wechat_50_reward_video_show_ad"
        const val POP_WECHAT_50_REWARD_VIDEO_SCENE = "pop_wechat_50_reward_video_scene"
        const val POP_WECHAT_50_REWARD_VIDEO_AD_MAX = 3
    }

    init {
        setBackgroundDrawable(null)
        val view = LayoutInflater.from(context).inflate(R.layout.pop_wechat_50__reward_video, null)
        contentView = view
        bind = PopWechat50RewardVideoBinding.bind(view)
        bind.rv.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)

        bind.flClose.setOnClickListener {
            iPopBtnActionListener?.onPopActionClick(POP_WECHAT_50_REWARD_VIDEO_CLOSE)
            dismiss()
        }
        bind.btn.setOnClickListener {
            if (bind.btn.text.toString() == "绑定微信") {
                dismiss()
                iPopBtnActionListener?.onPopActionClick(POP_WECHAT_50_REWARD_VIDEO_BIND_WECHAT)
            } else if (bind.btn.text.toString() == "开心收下") {
                dismiss()
                iPopBtnActionListener?.onPopActionClick(POP_WECHAT_50_REWARD_VIDEO_GET)
            } else {
                iPopBtnActionListener?.onPopActionClick(POP_WECHAT_50_REWARD_VIDEO_SHOW_AD)
            }
        }
    }

    fun show(parent: View, rewardCnt: Int) {
        refreshView(rewardCnt)
        showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0)
    }

    fun refresh(rewardCnt: Int) {
        if (adapterWechat50 == null) {
            return
        }
        if (rewardCnt >= POP_WECHAT_50_REWARD_VIDEO_AD_MAX) {
            val bean3 = adapterWechat50!!.getItem(3)

            bean3!!.spanStr = ""
            bean3.spanColor = ""
            bean3.tx1 = "观看%s个激励视频".format(POP_WECHAT_50_REWARD_VIDEO_AD_MAX)
            bean3.te1Color = textColor999
            val bean4 = adapterWechat50!!.getItem(4)
            bean4!!.tx1 = "奖励到账"
            bean4.te1Color = textColor999
            adapterWechat50?.notifyItemChanged(3, "refresh")
            adapterWechat50?.notifyItemChanged(4, "refresh")
            bind.btn.text = "开心收下"
        } else {
            val str1 = "观看%s个激励视频(".format(POP_WECHAT_50_REWARD_VIDEO_AD_MAX)
            adapterWechat50?.items?.forEachIndexed({ index, bean ->
                if (bean.tx1.contains(str1) && !bean.isDone) {

                    bean.tx1 = "观看%s个激励视频(%s/%s)".format(
                        POP_WECHAT_50_REWARD_VIDEO_AD_MAX,
                        rewardCnt,
                        POP_WECHAT_50_REWARD_VIDEO_AD_MAX
                    )
                    bean.spanStr = rewardCnt.toString()
                    adapterWechat50?.notifyItemChanged(index,"refresh")
                    return@forEachIndexed
                }
            })
        }
    }

    private fun refreshView(rewardCnt: Int) {
        adapterWechat50 = AdapterWechat50()
        adapterWechat50?.animationCompleteListener = object : AdapterWechat50.AnimationCompleteListener {
            override fun onAllAnimationsComplete() {

            }
        }
        bind.rv.adapter = adapterWechat50
        val contentList = mutableListOf<BeanWechat50>()
        if (false) {
            contentList.add(BeanWechat50("发起奖励", te1Color = textColor333))
            contentList.add(BeanWechat50("绑定微信", te1Color = "#FFFFFF", isDone = false))
            bind.btn.text = "绑定微信"
        } else {
            contentList.add(BeanWechat50("发起奖励", te1Color = textColor999))
            contentList.add(
                BeanWechat50(
                    "确认账户信息:%s".format("嘎嘎"),
                    te1Color = textColor999
                )
            )
            contentList.add(BeanWechat50("发放到账户", te1Color = textColor999))
            if (rewardCnt >= POP_WECHAT_50_REWARD_VIDEO_AD_MAX) {
                contentList.add(BeanWechat50("观看%s个激励视频".format(POP_WECHAT_50_REWARD_VIDEO_AD_MAX), te1Color = textColor999))
                contentList.add(BeanWechat50("奖励到账".format(POP_WECHAT_50_REWARD_VIDEO_AD_MAX), te1Color = textColor999))
                bind.btn.text = "开心收下"
            } else {
                val str1 = "观看%s个激励视频(%s/%s)".format(POP_WECHAT_50_REWARD_VIDEO_AD_MAX, rewardCnt, POP_WECHAT_50_REWARD_VIDEO_AD_MAX)
                contentList.add(
                    BeanWechat50(
                        str1,
                        te1Color = textColor333,
                        spanStr = rewardCnt.toString(),
                        spanColor = "#FF522B",
                        isDone = false
                    )
                )
                contentList.add(BeanWechat50("奖励到账".format(POP_WECHAT_50_REWARD_VIDEO_AD_MAX), te1Color = textColor999, isStepDone = false))
                bind.btn.text = "去试试，马上领取"
            }
        }
        adapterWechat50?.submitList(contentList)
        adapterWechat50?.startAnime()
    }

    private fun getTv4Text(num: Int): SpannableStringBuilder {
        val spannableStringBuilder = SpannableStringBuilder()
        val source = "观看3个激励视频(%s/%s)".format(num, POP_WECHAT_50_REWARD_VIDEO_AD_MAX)
        val numIndex = source.indexOf(num.toString())
        val numForegroundColorSpan = ForegroundColorSpan(Color.parseColor("#FF522B"))
        spannableStringBuilder.append(source)
        spannableStringBuilder.setSpan(
            numForegroundColorSpan, numIndex, numIndex + 1,
            Spanned.SPAN_INCLUSIVE_INCLUSIVE
        )
        return spannableStringBuilder
    }

    private fun startRotation(view: View) {
        val rotateAnimation = RotateAnimation(
            0f, 360f, // 从0度旋转到360度
            Animation.RELATIVE_TO_SELF, 0.5f, // 旋转中心X（相对于自身宽度的50%）
            Animation.RELATIVE_TO_SELF, 0.5f  // 旋转中心Y（相对于自身高度的50%）
        ).apply {
            duration = 2000L // 设置动画持续时间（毫秒）
            repeatCount = Animation.INFINITE // 设置无限循环
            interpolator = LinearInterpolator() // 设置线性插值器
            fillAfter = true // 动画结束后保持最后状态
        }
        view.startAnimation(rotateAnimation)
    }

    override fun onAllAnimationsComplete() {

    }

}